package com.technical.services;

import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import com.dao.ActionDAO;
import com.dao.AutomaticInterventionDAO;
import com.dao.MessageDAO;
import com.dao.impl.ActionDAOImpl;
import com.dao.impl.AutomaticInterventionDAOImpl;
import com.dao.impl.MessageDAOImpl;
import com.rev.Action;
import com.rev.Automaticintervention;
import com.rev.Message;
import com.technical.referentials.AutomaticInterventionReferentialPath;

@Path(AutomaticInterventionReferentialPath.INTERVENTION_PATH)
public class AutomaticInterventionServices {
	@Context
	UriInfo uriInf;
	 public static Logger log = Logger.getLogger("log_file");
	
	private AutomaticInterventionDAO aiDAO;
	private ActionDAO aDAO;
	private MessageDAO mDAO;
	
	public AutomaticInterventionServices() {
		aiDAO = new AutomaticInterventionDAOImpl();
		aDAO = new ActionDAOImpl();
		mDAO = new MessageDAOImpl();
		
		 FileHandler hand;
			try {
				hand = new FileHandler("ServerLog.txt");
				log.addHandler(hand);
				SimpleFormatter formatter=new SimpleFormatter();
				hand.setFormatter(formatter);
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			
			log.info("[Server] : Start");
	}
	
	@GET
	@Path(AutomaticInterventionReferentialPath.INTERVENTION_RESOURCE_PATH + AutomaticInterventionReferentialPath.INTERVENTION_RESOURCE_ID_PATH + "/{id}")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Automaticintervention getById(@PathParam("id") String id){
		log.info("[Server] : Method GET is called for th intervention with the id [" + id + "]");
		if (id == null)
			return null;
		Automaticintervention intervention = new Automaticintervention();
		intervention = new Automaticintervention();
		intervention = aiDAO.findById(Integer.parseInt(id));
		log.info("[Server] : Method GET Response [" + intervention.getAutomaticInterventionRef() + ")");  
		return intervention;
	}
	
	@POST
	@Path(AutomaticInterventionReferentialPath.INTERVENTION_RESOURCE_PATH)
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response insertIntervention(Automaticintervention intervention){
		//		log.info("[Server] : Method POST is called for the intervention [" + intervention.getAutomaticInterventionRef() + "]");

		if (intervention == null)
			return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
		aDAO.insert(intervention.getAction());
		mDAO.insert(intervention.getMessage());
		aiDAO.insert(intervention);
	//	log.info("[Server] : The Intervention is inserted in the database");
		UriBuilder ub = uriInf.getAbsolutePathBuilder();
		URI createdUri = ub.path(AutomaticInterventionReferentialPath.INTERVENTION_RESOURCE_ID_PATH + "/" + intervention.getIdAutomaticIntervention()).build();
		return Response.created(createdUri).build();
		}

	@GET
	@Path(AutomaticInterventionReferentialPath.INTERVENTION_RESOURCE_PATH + AutomaticInterventionReferentialPath.INTERVENTION_RESOURCE_MESSAGE_ID_PATH + "/{messageId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Action getMessageById(@PathParam("messageId") String messageId){
		List<Automaticintervention> interventions = aiDAO.findAll();
		if(interventions == null)
			return null;
		int msgId = Integer.parseInt(messageId);
		Action action = null;
		for (int i = 0; i < interventions.size(); i++) {
			if(interventions.get(i).getMessage().getIdMessage() == msgId)
				action = interventions.get(i).getAction();				
		}
		return action;
	}
	
	@GET
	@Path(AutomaticInterventionReferentialPath.INTERVENTION_RESOURCE_PATH + AutomaticInterventionReferentialPath.INTERVENTION_RESOURCE_ACTION_ID_PATH + "/{actionId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Message getByActionId(@PathParam("actionId") String actionId){

//		log.info("[Server] : Method GET of the Message for the Action with the id [" + actionId + "]");
		List<Automaticintervention> interventions = aiDAO.findAll();
		if(interventions == null)
			return null;
		int actId = Integer.parseInt(actionId);
		Message message = null;
		for (int i = 0; i < interventions.size(); i++) {
			if(interventions.get(i).getAction().getIdAction() == actId)
				message = interventions.get(i).getMessage();				
		}
	//	log.info("[Server] : Method GET Response [" + message.getMessageRef() + "]");
		return message;
	}
	
}
